package com.hn.project.rqinspect.service.impl;

import cn.hutool.core.util.StrUtil;
import com.hn.common.exception.BusinessException;
import com.hn.common.utils.CommonUtils;
import com.hn.common.utils.sql.SqlUtil;
import com.hn.project.common.domain.database.SysDictData;
import com.hn.project.common.mapper.SysDictDataMapper;
import com.hn.project.rqinspect.domain.database.*;
import com.hn.project.rqinspect.domain.dto.QuesPartItemDto;
import com.hn.project.rqinspect.domain.vo.QuesPartItemVO;
import com.hn.project.rqinspect.mapper.*;
import com.hn.project.rqinspect.service.QuesService;
import com.hn.utils.AssertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class QuesServiceImpl implements QuesService {

    @Autowired
    private QuesPartItemMapper quesPartItemMapper;

    @Autowired
    private TaskCheckRecordMapper taskCheckRecordMapper;

    @Autowired
    private TaskRecordMapper taskRecordMapper;

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private SysDictDataMapper dictDataMapper;

    @Override
    public List<QuesPartItemDto> listQuesPartItem(QuesPartItemVO quesPartItemVO) {
        Integer taskRecordId = quesPartItemVO.getTaskRecordId();
        TaskRecord taskRecord = taskRecordMapper.selectByPrimaryKey(taskRecordId);
        AssertUtils.notNull(taskRecord, "任务不存在");

        Integer taskId = taskRecord.getTaskId();
        Task task = taskMapper.selectByPrimaryKey(taskId);
        AssertUtils.notNull(task, "主任务不存在");

        quesPartItemVO.setQuesId(task.getQuesId());
        List<QuesPartItem> quesPartItems = quesPartItemMapper.listQuesPartItem(quesPartItemVO);

        Integer checkRecordId = quesPartItemVO.getCheckRecordId();
        getCheckFlag(checkRecordId, quesPartItems);

        Map<Integer, List<QuesPartItem>> collect = quesPartItems.stream()
                .filter(x -> x.getCheckType() != null)
                .collect(Collectors.groupingBy(QuesPartItem::getCheckType));

        List<QuesPartItemDto> quesPartItemDtos = new ArrayList<>();
        for (Map.Entry<Integer, List<QuesPartItem>> integerListEntry : collect.entrySet()) {
            QuesPartItemDto quesPartItemDto = new QuesPartItemDto();
            String checkTypeName = dictDataMapper.getLabelByVal("check_type", integerListEntry.getKey());
            quesPartItemDto.setCheckType(integerListEntry.getKey());
            quesPartItemDto.setCheckTypeName(checkTypeName);
            quesPartItemDto.setQuesPartItemDtoList(integerListEntry.getValue());
            quesPartItemDtos.add(quesPartItemDto);
        }
        return quesPartItemDtos;
    }

    private void getCheckFlag(Integer checkRecordId, List<QuesPartItem> quesPartItems) {
        if (checkRecordId == null || quesPartItems == null || quesPartItems.isEmpty()) {
            return;
        }

        TaskCheckRecord taskCheckRecord = taskCheckRecordMapper.selectByPrimaryKey(checkRecordId);
        if (taskCheckRecord == null) {
            return;
        }

        String checkPartItems = taskCheckRecord.getCheckPartItems();
        if (StrUtil.isBlank(checkPartItems)) {
            return;
        }

        // 判断当前检查项是否已经检查过了
        quesPartItems.stream()
                .filter(x -> CommonUtils.contains(x.getId(), checkPartItems))
                .forEach(x -> x.setCheckFlag(1));
    }

}
